﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using DevExpress.Web.Mvc;
using EnterpriseFund.Common;
using Newtonsoft.Json;
using DevExpress.Web.ASPxGridView;
using System.Web.UI;
using EnterpriseFund.BLL;
using DevExpress.XtraReports.UI;
using DevExpress.XtraPrinting;

namespace EnterpriseFund.Controllers
{
    public class QueryAllowancePayController : Controller
    {
        //
        // GET: /QueryAllowancePay/

        public ActionResult Index()
        {
            EnterpriseFund.BLL.QueryAllowancePay QueryAllowancePaybll = new EnterpriseFund.BLL.QueryAllowancePay();
            var zsxx = QueryAllowancePaybll.GetModelList("");
            foreach (Model.QueryAllowancePay x in zsxx)
            {
                x.FAuditAmount = decimal.Parse(x.FAuditAmount.ToString("f2"));
               
            }
            BLL.FinanceProp f = new BLL.FinanceProp();
            var vf = f.GetAllList();
            ViewData["FinancePropList"] = vf;

           // return View("Index", zsxx);
            return View();
        }
        #region 导出EXCEL
        public ActionResult ExportToXls()
        {
            var table = GetData(GetFetchString());
            return GridViewExtension.ExportToXls(ExportGridViewSettings, table, HttpUtility.UrlEncode("资金拨付查询导出.xls"), true);
        }

        static GridViewSettings exportGridViewSettings;
        public static GridViewSettings ExportGridViewSettings
        {

            get
            {

                if (exportGridViewSettings == null)

                    exportGridViewSettings = CreateExportGridViewSettings();
                return exportGridViewSettings;
            }
        }
        private string IsNull(string s1, string s2)
        {
            return string.IsNullOrEmpty(s1) ? s2 : s1;
        }
        //public string ClearViewBag()
        //{
        //    try
        //    {
        //        ViewBag=null;
        //        return "OK";
        //    }
        //    catch (System.Exception ex)
        //    {
        //        return "error";
        //    }
        //}
        public string GetFetchString()
        {
            var nsrsbh = Fetch.Post("hnsrsbh");
            var nsrbm = Fetch.Post("hnsrbm");
            var nsrmc = Fetch.Post("hnsrmc");

            var FFinance = IsNull(ComboBoxExtension.GetValue<string>("FFinance"), Fetch.Post("hFFinance"));
            var FAmount1 = Fetch.Post("hFAmount1");
            var FAmount2 = Fetch.Post("hFAmount2");
            var FFileNo = Fetch.Post("hFFileNo");
            var FNote = Fetch.Post("hFNote");
            var FPayDateBegin = Fetch.Post("hFPayDateBegin");
            var FPayDateEnd = Fetch.Post("hFPayDateEnd");
            ViewData["FFinance"] = FFinance;
            ViewData["FAmount1"] = FAmount1;
            ViewData["FAmount2"] = FAmount2;
            ViewData["FFileNo"] = FFileNo;
            ViewData["FNote"] = FNote;
            ViewData["FPayDateBegin"] = FPayDateBegin;
            ViewData["FPayDateEnd"] = FPayDateEnd;
            ViewData["nsrbm"] = nsrbm;
            ViewData["nsrmc"] = nsrmc;
            ViewData["nsrsbh"] = nsrsbh;
            ViewData["FFinance"] = FFinance;
            var filter = filters(nsrsbh, nsrbm, nsrmc, FFinance, FAmount1, FAmount2, FFileNo, FNote, FPayDateBegin, FPayDateEnd);

            return filter;
        }
        public string GetWhereString()
        {
            //绑定值

            //var FTaxClassAllowanceName = IsNull(ComboBoxExtension.GetValue<string>("FTaxClassAllowanceName"), Request["FTaxClassAllowanceName"]);
            var nsrsbh = Request["Nsrsbh"];
            var nsrbm = Request["Nsrbm"];
            var nsrmc = Request["Nsrmc"];
          
            var FFinance = IsNull(ComboBoxExtension.GetValue<string>("FFinance"), Request["FFinance"]);
            var FAmount1 = Request["FAmount1"];
            var FAmount2 = Request["FAmount2"];
            var FFileNo = Request["FFileNo"];
            var FNote = Request["FNote"];
            var FPayDateBegin = Request["FPayDateBegin"];
            var FPayDateEnd = Request["FPayDateEnd"];
            //var FPayDateBegin = DateEditExtension.GetValue<string>("FPayDateBegin1");
            //var FPayDateEnd = DateEditExtension.GetValue<string>("FPayDateEnd1");
            //  var = Request[""];
            ViewBag.FFinance = FFinance;
            ViewBag.FAmount1 = FAmount1;
            ViewBag.FAmount2 = FAmount2;
            ViewBag.FFileNo = FFileNo;
            ViewBag.FNote = FNote;
            ViewBag.FPayDateBegin = FPayDateBegin;
            ViewBag.FPayDateEnd = FPayDateEnd;
            ViewBag.nsrbm = nsrbm;
            ViewBag.nsrmc = nsrmc;
            ViewBag.nsrsbh = nsrsbh;
            ViewBag.FFinance = FFinance;
            var filter = filters(nsrsbh, nsrbm, nsrmc, FFinance, FAmount1, FAmount2, FFileNo, FNote, FPayDateBegin, FPayDateEnd);

            return filter;

        }
        public ActionResult ReportViewerPartial()
        {
            var strWhere = GetWhereString();
            var zsxx = GetData(strWhere);
            MVCReportGeneratonHelper generator = new MVCReportGeneratonHelper();
            generator.CustomizeColumnsCollection += new CustomizeColumnsCollectionEventHandler(generator_CustomizeColumnsCollection);
            generator.CustomizeColumn += new CustomizeColumnEventHandler(generator_CustomizeColumn);
            XtraReport report = generator.GenerateMVCReport(OperateSession.GetFromCache("QueryAllowancePay") as MVCxGridViewState, zsxx);
            //  XtraReport report = generator.GenerateMVCReport(gridViewState, dt);

            //report.PaperKind = System.Drawing.Printing.PaperKind.A4;

            this.ViewData["rpt"] = report;

            return PartialView("ReportViewerPartial");

        }
        void generator_CustomizeColumnsCollection(object source, ColumnsCreationEventArgs e)
        {
            for (int i = 0; i < e.ColumnsInfo.Count; i++)
            {

                if (e.ColumnsInfo[i].FieldName == "NSRBM")
                {
                    e.ColumnsInfo[i].ColumnWidth = 80;
                }
                if (e.ColumnsInfo[i].FieldName == "NSRMC")
                {
                    e.ColumnsInfo[i].ColumnWidth = 320;
                }
                if (e.ColumnsInfo[i].FieldName == "FFileNo")
                {
                    e.ColumnsInfo[i].ColumnWidth =60;

                }
                if (e.ColumnsInfo[i].FieldName == "NSRSBH")
                {
                    e.ColumnsInfo[i].ColumnWidth =  150;
                }
                if (e.ColumnsInfo[i].FieldName == "FFinancePropShortName")
                {
                    e.ColumnsInfo[i].ColumnWidth = 80;
                }
                if (e.ColumnsInfo[i].FieldName == "FAuditAmount")
                {
                    e.ColumnsInfo[i].ColumnWidth = 100;
                }
               if (e.ColumnsInfo[i].FieldName == "FPayDateStr")
                {
                    e.ColumnsInfo[i].ColumnWidth = 80;
                }
               if (e.ColumnsInfo[i].FieldName == "FNote")
                {
                    e.ColumnsInfo[i].ColumnWidth = 300 ;
                }

            }
           
            
        }
        void generator_CustomizeColumn(object source, ControlCustomizationEventArgs e)
        {
            if (e.FieldName == "FAuditAmount")
            {
                
                XRTableCell c = e.Owner as XRTableCell;
                c.TextAlignment = TextAlignment.MiddleRight;
               
            }

            if (e.FieldName == "NSRSBH")
            {

                XRTableCell c = e.Owner as XRTableCell;
                c.TextAlignment = TextAlignment.MiddleLeft;

            }
            if (e.FieldName == "NSRMC")
            {

                XRTableCell c = e.Owner as XRTableCell;
                c.TextAlignment = TextAlignment.MiddleLeft;

            }
        }
        
        public ActionResult Print()
        {
            GetFetchString();
            // CreatReport(table);
            return View("Print");
        }
        private static string filters(string nsrsbh, string nsrbm, string nsrmc, string FFinance, string FAmount1, string FAmount2, string FFileNo, string FNote, string FPayDateBegin, string FPayDateEnd)
        {
            var filter = "1=1";
            if (!string.IsNullOrEmpty(nsrsbh))
                filter += string.Format("and NSRSBH like '%{0}%'", nsrsbh);
            if (!string.IsNullOrEmpty(nsrbm))
                filter += string.Format("and NSRBM like '%{0}%'", nsrbm);
            if (!string.IsNullOrEmpty(nsrmc))
                filter += string.Format("and NSRMC like '%{0}%'", nsrmc);
            if (!string.IsNullOrEmpty(FNote))
                filter += string.Format("and FNote like '%{0}%'", FNote);

            if (!string.IsNullOrEmpty(FFinance))
            {
                filter += string.Format(" and FFinancePropNo = '{0}' ", FFinance);
            }
            //if (!string.IsNullOrEmpty(FAmount))
            //{
            //    filter += string.Format(" and FAmount = '{0}' ", FAmount);
            //}
            if (!string.IsNullOrEmpty(FAmount1) && !string.IsNullOrEmpty(FAmount2 ))
            {
                filter += " and FAmount >=" + FAmount1 + " and FAmount<=" + FAmount2 + " ";
            }
            else if (!string.IsNullOrEmpty(FAmount1))
            {
                filter += " and FAmount >=" + FAmount1;
            }
            else if (!string.IsNullOrEmpty(FAmount2))
            {
                filter += " and FAmount <=" + FAmount2;
            }
            if (!string.IsNullOrEmpty(FFileNo))
            {
                filter += string.Format(" and FFileNo = '{0}' ", FFileNo);
            }

            if (!string.IsNullOrEmpty(FPayDateBegin) || !string.IsNullOrEmpty(FPayDateEnd))
            {
                var begin = string.IsNullOrEmpty(FPayDateBegin) ? DateTime.MinValue.ToString() : FPayDateBegin;
                var end = string.IsNullOrEmpty(FPayDateEnd) ? DateTime.MaxValue.ToString() : FPayDateEnd;
                filter += string.Format(" AND FPayDate <= '{0}'", end);
                if (!string.IsNullOrEmpty(FPayDateBegin))
                    filter += string.Format(" and FPayDate >= '{0}'", begin);
            }
            return filter;
        }
        public List<Model.QueryAllowancePay> GetModelVSBZSXX()
        {
            var strWhere = GetWhereString();
            List<Model.QueryAllowancePay> zsxx = GetData(strWhere);
            return zsxx;
        }

        private static List<Model.QueryAllowancePay> GetData(string strWhere)
        {
            EnterpriseFund.BLL.QueryAllowancePay QueryAllowancePaybll = new EnterpriseFund.BLL.QueryAllowancePay();
            List<Model.QueryAllowancePay> zsxx = QueryAllowancePaybll.GetModelList(strWhere);
            foreach (Model.QueryAllowancePay x in zsxx)
            {
                x.FAuditAmount = decimal.Parse(x.FAuditAmount.ToString("f2"));
            }
            return zsxx;
        }
        public static GridViewSettings CreateExportGridViewSettings()
        {
            var setting = new GridViewSettings();
            setting.Name = "gvEditing";
            setting.KeyFieldName = "FApplicationFormID";
            setting.CallbackRouteValues = new { Controller = "QueryAllowancePay", Action = "QueryAllowancePayListPartial" };
            setting.Width = System.Web.UI.WebControls.Unit.Percentage(100);
            setting.SettingsPager.PageSize = EnterpriseFund.Common.GlobalConstant.CONSTRING_PAGESIZE;
            setting.Columns.Add("NSRBM", "纳税人编码");
            setting.Columns.Add("NSRMC", "企业名称");
            setting.Columns.Add("FFileNo", "文件号");
            setting.Columns.Add("NSRSBH", "纳税人识别号");

            setting.Columns.Add("FFinancePropShortName", "资金性质");
            setting.Columns.Add("FAuditAmount", "补贴金额").CellStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Right;
            setting.Columns.Add("FPayDateStr", "业务生成日期");
            setting.Columns.Add("FNote", "备注");
            setting.SettingsBehavior.AllowFocusedRow = true;
            setting.SettingsLoadingPanel.Text = "数据读取中...";
            setting.SettingsExport.Styles.Header.BackColor = System.Drawing.Color.White;
            setting.SettingsExport.Styles.Header.ForeColor = System.Drawing.Color.Black;
            setting.TotalSummary.Add(DevExpress.Data.SummaryItemType.Count, "NSRBM").DisplayFormat = "总计:";
            setting.TotalSummary.Add(DevExpress.Data.SummaryItemType.Sum, "FAuditAmount").DisplayFormat = "￥{0:f2}";
           
            setting.GroupSummary.Add(DevExpress.Data.SummaryItemType.Sum, "FAuditAmount").DisplayFormat = "合计:￥{0}";
            setting.Settings.ShowGroupPanel = true;
            setting.Settings.ShowFooter = true;
            setting.ClientSideEvents.BeginCallback = "function(s,e){OnBeginCallback(s,e)}";
            setting.ClientSideEvents.EndCallback = "function(){}";
            setting.Columns["FAuditAmount"].FooterCellStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Right;
            setting.SettingsBehavior.EnableCustomizationWindow = true;
			
			
            setting.DataBinding = (sender, e) =>
            {
                ASPxGridView grid = (ASPxGridView)sender;
                for (int i = 0; i < grid.AllColumns.Count; i++)
                {
                    if (grid.AllColumns[i].HeaderStyle.HorizontalAlign == System.Web.UI.WebControls.HorizontalAlign.NotSet)
                    {
                        grid.AllColumns[i].HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                    }
                }
               
            };
            setting.PreRender = setting.BeforeGetCallbackResult = (sender, e) =>
            {


                MVCxGridView gridView = sender as MVCxGridView;
                MVCxGridViewState gridViewState = new MVCxGridViewState(gridView);
                OperateSession.SetToCache(gridViewState, "QueryAllowancePay");


            };
            return setting;
        }
        public ActionResult QueryAllowancePayListPartial()
        {
            GetWhereString();
            var vsbzsxx = GetModelVSBZSXX();
            return View(vsbzsxx);
        }
        #endregion

    }
}
